"use strict";
var vue = new Vue({
    el: '#app',
    data() {

        return {
            vueHeader: {},
            noticeList: [],
            // 控制公告管理对话框是否可见
            noticeManageDialogVisible: false,
            // 公告是否正在修改中
            isNoticeChanging: false,
            // 公告对话框的宽度
            noticeManageDialogWidth: "50%",
            // 公告对话框按钮大小
            dialogFontSize: "18px",
            // 添加公告表单的数据
            addNoticeFormData: {
                title: "",
                content: ""
            },
            // 添加公告表单的校验规则
            addNoticeFormRules: {
                title: [
                    { required: true, message: "请输入标题", trigger: "blur" }
                ],
                content: [
                    { required: true, message: "请输入内容", trigger: "blur" }
                ]
            },
            test: "test"
        }
    },
    //钩子函数，VUE对象初始化完成后自动执行
    created() {
        // 获取公告
        getNoticeListFromServer(this);
    },
    mounted() {

    },
    methods: {
        /**
         * 立即更新公告缓存
         */
        updateNoticeRedis() {
            // 设置对话框不可用
            this.isNoticeChanging = true;
            axios.get(addressNCUpdateRedis).then((result) => {
                if (result.data.flag) {
                    // 重新获取公告列表
                    getNoticeListFromServer(this, () => {
                        this.$notify({ title: "成功", message: result.data.message, type: "success" });
                        setTimeout(() => {
                            // 对话框可用
                            this.isNoticeChanging = false;
                            // 关闭对话框
                            this.noticeManageDialogVisible = false;
                        }, "1000");
                    });
                } else {
                    this.$notify.error({ title: "错误", message: result.data.message });
                    // 对话框可用
                    this.isNoticeChanging = false;
                }
            });
            // 关闭对话框
            this.noticeManageDialogVisible = false;
        },
        // 提交表单
        sendNotice(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.isNoticeChanging = true;
                    // 发送请求
                    this.$notify.info({ title: "提示", message: "正在修改，请稍后！" });
                    axios.post(addressNCAdd, this.addNoticeFormData).then((result) => {
                        if (result.data.flag) {
                            // 重新获取公告列表
                            getNoticeListFromServer(this, () => {
                                this.$notify({ title: "成功", message: result.data.message, type: "success" });
                                setTimeout(() => {
                                    // 对话框可用
                                    this.isNoticeChanging = false;
                                    // 清空表单并关闭对话框
                                    this.resetForm(formName);
                                }, "1000");
                            });
                        } else {
                            this.$notify.error({ title: "错误", message: result.data.message });
                            this.isNoticeChanging = false;
                        }
                    });
                } else {
                    console.log("表单校验未通过！");
                    this.isNoticeChanging = false;
                    return false;
                }
            });
        },
        // 清空表单并关闭对话框
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.noticeManageDialogVisible = false;
        },
        // 注意，此方法由vueHeader在创建时自动调用，参数为vueHeader自身对象
        // 可以在vueHeader对象中获取到它的所有信息
        vueHeaderCreated(vueHeader) {
            this.vueHeader = vueHeader;
        },
        // 屏幕大小变化事件，由vueHeader自动调用
        vueHeaderOnresize(screenWidth, screenHeight) {
            if (screenWidth < 992) {
                this.noticeManageDialogWidth = "95%";
                this.dialogFontSize = "12px";
            } else if (screenWidth < 1200) {
                this.noticeManageDialogWidth = "60%";
                this.dialogFontSize = "14px";
            } else {
                this.noticeManageDialogWidth = "50%";
                this.dialogFontSize = "16px";
            }
        }
    }
});